
import localeData from "@/locales/countries"
// import API from "@/api";
export const shorts = {
  data() {
    var checkFileId = (rule, value, callback) => {
      if (!this.form.coverPictureId) {
        return callback(new Error(this.$t('formRule.file_rule')));
      } else {
        callback()
      }
    };
    var checkVideoFileId = (rule, value, callback) => {
      if (!this.form.previewId) {
        return callback(new Error(this.$t('formRule.file_rule')));
      } else {
        callback()
      }
    };
    var checkViewAreas = (rule, value, callback) => {
      if (this.form.viewAreas.length == 0) {
        return callback(new Error(this.$t('formRule.selectPlaceholder')));
      } else {
        callback()
      }
    };
    
    var checkDis = (rule, value, callback) => {
      if (!this.form.discount || !this.form.discount.length || this.form.discount.findIndex(n=>!n.episodeRate) >= 0 || this.form.discount.findIndex(n=>!n.rebate) >= 0) {
        return callback(new Error(this.$t('formRule.selectPlaceholder')));
      } else if(this.form.discount.findIndex(n=>n.episodeRate<0) >= 0 || this.form.discount.findIndex(n=>n.rebate<0) >= 0) {
        return callback(new Error(this.$t('formRule.discountPlaceholder')));
      } else {
        callback()
      }
    };
    var checkTags = (rule, value, callback) => {
      if (!this.form.tags || this.form.tags.length == 0) {
        return callback(new Error(this.$t('formRule.selectPlaceholder')));
      } else {
        callback()
      }
    };
    var checkProductions = (rule, value, callback) => {
      if (!this.form.productions || this.form.productions.length == 0) {
        return callback(new Error(this.$t('formRule.selectPlaceholder')));
      } else {
        callback()
      }
    };
    return {
      mode: "add",
      defaultTime: [new Date(2000,1,1,23,59,59)],
      showProp: false,
      disabled: false,
      action: '#',
      previewAction: '#',
      showUpload: true,
      showAddBtn: true,
      shortsTotal: 41,
      episodes: 1,
      ageRatingList: [
        {
          type: 1,
          label: 'G'
        },
        {
          type: 2,
          label: 'PG12'
        },
        {
          type: 3,
          label: 'R15+'
        },
        {
          type: 4,
          label: 'R18+'
        }
      ],
      // 短剧状态连载。。。完结
      // statusList: [
      //   {
      //     type: 1,
      //     label: this.$t('short_data.status1')
      //   },
      //   {
      //     type: 2,
      //     label: this.$t('banner_data.status2')
      //   },
      //   {
      //     type: 3,
      //     label: this.$t('banner_data.status3')
      //   }
      // ],
      // 短剧状态连载。。。完结
      completedList: [
        {
          type: 1,
          label: this.$t('short_data.status.running')
        },
        {
          type: 2,
          label: this.$t('short_data.status.completed')
        }
      ],
      currencyType: [
        // {
        //   value: 1,
        //   name: this.$t('table_data.currency.jpy'),
        // },
        // {
        //   value: 2,
        //   name: this.$t('table_data.currency.dollar'),
        // },
        // {
        //   value: 3,
        //   name: this.$t('table_data.currency.rmb'),
        // },
        {
          value: 4,
          name: this.$t('table_data.currency.mov'),
        },
      ],
      movTypeList: [],
      movType: [],
      productionsList: [],
      productions: [],
      tagsList: [],
      tags: [],
      disList: {},
      localeList: [
        {
          country: 'ALL',
          en_US: 'ALL',
          ja_JP: '全て',
          zh_CN: '全部',
          zh_TW: '全部',
        }
      ],
      viewLocaleList: [
        // {
        //   "areas": {
        //     "provinceId": "",
        //     "provinceName": {"en_US": "", "ja_JP": "", "zh_CN": "", "zh_TW": "" },
        //     "cityId": "",
        //     "cityName": {"en_US": "", "ja_JP": "", "zh_CN": "", "zh_TW": "" },
        //     "areaId": "",
        //     "areaName": {"en_US": "", "ja_JP": "", "zh_CN": "", "zh_TW": "" }
        //   },
        //   "countryName": {"en_US": "ALL", "ja_JP": "全て", "zh_CN": "全部", "zh_TW": "全部" },
        //   "countryCode": "*"
        // }
      ],
      tableList: [],
      vhTypeList: [
        {
          name: this.$t('short_data.video.typeHscreen'),
          value: 1
        },
        {
          name: this.$t('short_data.video.typeVscreen'),
          value: 2
        }
      ],
      creatorIdList: [],
      categoryList: [],
      viewAreas: ["*"],
      form: {
        allEpisodesStatus: false,
        restrictStatus: 1,
        movieGenreId: null,
        productions: [],
        tags: [],
        previewUrl: '',
        previewId: '',
        category: '',
        createBy: '',
        vhType: 1,
        previewVhType: 1,
        shortId: '',
        grossPrice: 0,
        shortName: {en_US: '',ja_JP: '',zh_CN: '',zh_TW: ''},
        description: {en_US: '',ja_JP: '',zh_CN: '',zh_TW: ''},
        intro: {en_US: '',ja_JP: '',zh_CN: '',zh_TW: ''},
        updateWeekday: {en_US: '',ja_JP: null,zh_CN: null,zh_TW: null},
        shortLanguage: '',
        originalDate: '',
        isWarnings: false,
        ageRating: '',
        episode: '',
        director: '',
        writer: '',
        cast: '',
        directors: {en_US: '', ja_JP: null,zh_CN: null,zh_TW: null},
        writers: {en_US: '', ja_JP: null,zh_CN: null,zh_TW: null},
        casts: {en_US: '', ja_JP: null,zh_CN: null,zh_TW: null},
        movieId: '',
        saleArea: '',
        viewAreas: [{
          "areas": "{\"provinceId\":\"\",\"provinceName\":{\"en_US\":\"\",\"ja_JP\":\"\",\"zh_CN\":\"\",\"zh_TW\":\"\"},\"cityId\":\"\",\"cityName\":{\"en_US\":\"\",\"ja_JP\":\"\",\"zh_CN\":\"\",\"zh_TW\":\"\"},\"areaId\":\"\",\"areaName\":{\"en_US\":\"\",\"ja_JP\":\"\",\"zh_CN\":\"\",\"zh_TW\":\"\"}}",
          "countryName": {"en_US": "ALL", "ja_JP": "全て", "zh_CN": "全部", "zh_TW": "全部" },
          "countryCode": "*",
          "restrictStatus": 1
        }],
        startDate: '',
        endDate: '',
        openDate: '',
        currency: '',
        sort: '',
        discount: [],
        coverPicture: '',
        coverPictureId: ''
      },
      uploadForm: {
        movie_id: '',
        start_date: '',
        end_date: '',
        official_site: '',
        information: '',
        fileUrl: null,
        thumbnailUrl: null,
        title: '',
      },
      draftForm: {},
      allForm: {},
      rules: {
        "category": [
          {required: true, message: this.$t('formRule.selectPlaceholder'), trigger:'change'}
        ],
        "movieGenreId" : [
          {required: true, message: this.$t('formRule.selectPlaceholder'), trigger:'change'}
        ],
        "productions": [
          {required: true, validator: checkProductions, trigger:'change'}
        ],
        "creatorId": [
          {required: true, message: this.$t('formRule.selectPlaceholder'), trigger:'change'}
        ],
        "previewVhType": [
          {required: true, message: this.$t('formRule.selectPlaceholder'), trigger:'change'}
        ],
        "vhType": [
          {required: true, message: this.$t('formRule.selectPlaceholder'), trigger:'change'}
        ],
        "shortName.en_US": [
          {required: true,message: this.$t('formRule.inputPlaceholder'),trigger: 'change'}
        ],
        "description.en_US": [
          {required: true,message: this.$t('formRule.inputPlaceholder'),trigger: 'change'}
        ],
        "name": [
          {required: true,message: this.$t('formRule.inputPlaceholder'),trigger: 'change'}
        ],
        "status": [
          {required: true,message: this.$t('formRule.selectPlaceholder'),trigger: 'change'}
        ],
        "completed": [
          {required: true,message: this.$t('formRule.selectPlaceholder'),trigger: 'change'}
        ],
        "shortLanguage": [
          {required: true,message: this.$t('formRule.inputPlaceholder'),trigger: 'change'}
        ],
        "originalDate": [
          {required: true,message: this.$t('formRule.selectPlaceholder'),trigger: 'change'}
        ],
        "ageRating": [
          {required: true,message: this.$t('formRule.selectPlaceholder'),trigger: 'change'}
        ],
        // "episode": [
        //   {required: true,message: this.$t('formRule.inputPlaceholder'),trigger: 'change'}
        // ],
        // "director": [
        //   {required: true,message: this.$t('formRule.inputPlaceholder'),trigger: 'change'}
        // ],
        // "writer": [
        //   {required: true,message: this.$t('formRule.inputPlaceholder'),trigger: 'change'}
        // ],
        // "cast": [
        //   {required: true,message: this.$t('formRule.inputPlaceholder'),trigger: 'change'}
        // ],
        "episode": [
          {required: true, message: this.$t('formRule.inputPlaceholder'), trigger: 'blur'}
        ],
        "createBy": [
          {required: true,message: this.$t('formRule.selectPlaceholder'),trigger: 'change'}
        ],
        "saleArea": [
          {required: true,message: this.$t('formRule.selectPlaceholder'),trigger: 'change'}
        ],
        "startDate": [
          {required: true,message: this.$t('formRule.selectPlaceholder'),trigger: 'change'}
        ],
        "endDate": [
          {required: true,message: this.$t('formRule.selectPlaceholder'),trigger: 'change'}
        ],
        "openDate": [
          {required: true,message: this.$t('formRule.selectPlaceholder'),trigger: 'change'}
        ],
        "currency": [
          {required: true,message: this.$t('formRule.selectPlaceholder'),trigger: 'change'}
        ],
        // "sort": [
        //   {required: true,message: this.$t('formRule.selectPlaceholder'),trigger: 'change'}
        // ],
        "discount": [
          {required: true, validator: checkDis, trigger:'change'}
        ],
        "tags": [
          {required: true, validator: checkTags, trigger:'change'}
        ],
        "coverPictureId": [
          {required: true, validator: checkFileId, trigger:'change'}
        ],
        "previewId": [
          {required: true, validator: checkVideoFileId, trigger:'change'}
        ],
        "viewAreas": [
          {required: true, validator: checkViewAreas, trigger:'change'}
        ],
        "directors.en_US": [
          {required: true,message: this.$t('formRule.inputPlaceholder'),trigger: 'change'}
        ],
        "writers.en_US": [
          {required: true,message: this.$t('formRule.inputPlaceholder'),trigger: 'change'}
        ],
        "casts.en_US": [
          {required: true,message: this.$t('formRule.inputPlaceholder'),trigger: 'change'}
        ],
      },
      visible: false,
      isSaveing: false
    }
  },
  watch: {
    "form.thumbnailId": {
    	handler: function (val) {
        if(val) {
          this.form.coverPicture = this.form.thumbnailUrl;
          this.form.coverPictureId = this.form.thumbnailId;
        } else {
          this.form.coverPicture = "";
          this.form.coverPictureId = "";
        }
    	},
    	immediate: true,
    	deep: true
    },
    // "scDialogData.thumbnailUrl": {
    // 	handler: function (val) {
    //     if(val) {
    //       this.form.coverPicture = this.form.thumbnailUrl;
    //       this.form.coverPictureId = this.form.thumbnailId;
    //     } else {
    //       this.form.coverPicture = "";
    //       this.form.coverPictureId = "";
    //     }
    // 	},
    // 	immediate: true,
    // 	deep: true
    // },
    "form.discount": {
    	handler: function (val) {
        this.showAddBtn = val.length >= Math.ceil(this.form.episode) ? false: true;
    	},
    	immediate: true,
    	deep: true
    },
    "form.episode": {
    	handler: function (val) {
        if(val&& !isNaN(val)){
          this.showAddBtn = this.form.discount.length >= Math.ceil(val) ? false: true;
          this.setSeries()
        }else if(val){
          this.$message.error(this.$t('short_data.video.selectEpisodeErr'))
        }
    	},
    	immediate: true,
    	deep: true
    }
  },
  created() {
    this.getLocale();
    this.getViewLocale();
  },
  methods: {
    uploadSuccessCallback(res) {
      this.form.coverPictureId = res;
      this.$message.success(this.$t('table_data.operasucc'))
    },
    horizonUploadSuccessCallback(res) {
      this.form.coverHorizontalPictureId = res;
      this.$message.success(this.$t('table_data.operasucc'))
    },
    handleRemovePoster(){
      this.form.coverPicture = "";
      this.form.coverPictureId = "";
      this.form.thumbnailId = "";
      this.form.thumbnailUrl = "";
      this.showUpload = true;
    },
    handleRemoveHorizonPoster() {
      this.form.coverHorizontalPicture = "";
      this.form.coverHorizontalPictureId = "";
      // this.form.thumbnailId = "";
      // this.form.thumbnailUrl = "";
      this.showUpload = true;
    },
    handleRemovePreview() {
      this.form.previewUrl = "";
      this.form.previewId = "";
    },
    getPosterFile(file){
      if(file.status === 'success') {
        // this.scDialogData.thumbnailUrl = file.tempFile
        this.form.coverPicture = file.tempFile;
        // this.image = file.tempFile;
      }
    },
    getHorizonPosterFile(file){
      if(file.status === 'success') {
        // this.scDialogData.thumbnailUrl = file.tempFile
        this.form.coverHorizontalPicture = file.tempFile;
        // this.image = file.tempFile;
      }
    },
    getPreviewFile(file){
      this.form.previewUrl = file.tempFile;
    },
    async headCallPoster(res){
      if(!res)return
      this.form.coverPictureId = res;
      this.form.coverPicture = await this.getRealImgUrl(res);
      this.$message.success(this.$t('banner_data.uploadSuccessText'))
    },
    async headCallPreview(res) {
      if(!res)return
      this.form.previewId = res;
      // this.form.previewVideoUrl = await this.getRealImgUrl(res);
      this.$message.success(this.$t('banner_data.uploadSuccessText'))
    },
    // 获取真实视频链接
    async getRealVideoUrl(url){
      const response = await this.$API.shorts.getVideoFileId.get(url);
      return response.data
    },
    // 获取真实图片链接
    async getRealImgUrl(url){
      const response = await this.$API.shorts.getFileId.get(url);
      return response.data
    },
    changeEips(){
      this.setSeries();
    },
    setSeries(){
      this.disList = {}
      for(let t=0;t<this.form.discount.length;t++){
        let index = this.form.discount[t].episodeNum;
        for(let i = 1;i <= this.form.episode; i++){
          if(!this.disList[index])this.disList[index] = [];
          if(i % this.episodes == 0 && this.form.discount.findIndex(n=>n.episodeNum==i) == -1)this.disList[index].push({name:i,value:i})
          if(i == this.form.episode && this.form.episode % this.episodes != 0 && this.form.discount[t].episodeNum != i && this.form.discount.findIndex(n=>n.episodeNum==i) == -1)this.disList[index].push({name: this.form.episode,value: this.form.episode})
        }
      }
    },
    delDiscount(index){
      this.form.discount.splice(index,1)
      this.setSeries();
    },
    addDiscount(){
      let arr = Math.ceil(this.form.episode / this.episodes);
      for(let i = 1;i <= this.form.episode; i++){
        if((i % this.episodes == 0 && this.form.discount.findIndex(n=>n.episodeNum == i) == -1) || this.form.discount.length+1 == arr && this.form.episode == i){
          this.form.discount.push({
            episodeNum: i,
            episodeRate: '',
            rebate: null
          })
          this.setSeries();
          break
        }
      }
    },
    searchArea(e){
      if(!e){
        this.localeList = [{
          country: 'ALL',
          en_US: 'ALL',
          ja_JP: '全て',
          zh_CN: '全部',
          zh_TW: '全部',
        }];
        this.getLocale();
        return
      }
      this.localeList = [this.localeList.find(n=>n[this.$i18n.locale].includes(e))];
    },
    async getLocale(){
      this.localeList = this.localeList.concat(localeData||[]);
    },
    searchViewArea(e){
      if(!e){
        // this.viewLocaleList = [
        //   {
        //     "areas": "{\"provinceId\":\"\",\"provinceName\":{\"en_US\":\"\",\"ja_JP\":\"\",\"zh_CN\":\"\",\"zh_TW\":\"\"},\"cityId\":\"\",\"cityName\":{\"en_US\":\"\",\"ja_JP\":\"\",\"zh_CN\":\"\",\"zh_TW\":\"\"},\"areaId\":\"\",\"areaName\":{\"en_US\":\"\",\"ja_JP\":\"\",\"zh_CN\":\"\",\"zh_TW\":\"\"}}",
        //     "countryName": {"en_US": "ALL", "ja_JP": "全て", "zh_CN": "全部", "zh_TW": "全部" },
        //     "countryCode": "*"
        //   }
        // ];
        this.getViewLocale();
        return
      }
      this.viewLocaleList = this.viewLocaleList.filter(n=>n.countryName[this.$i18n.locale].includes(e));
    },
    async getViewLocale(){
      this.viewLocaleList = [];
      const areasList = localeData.map(
        n => ({
          "areas": "{\"provinceId\":\"\",\"provinceName\":{\"en_US\":\"\",\"ja_JP\":\"\",\"zh_CN\":\"\",\"zh_TW\":\"\"},\"cityId\":\"\",\"cityName\":{\"en_US\":\"\",\"ja_JP\":\"\",\"zh_CN\":\"\",\"zh_TW\":\"\"},\"areaId\":\"\",\"areaName\":{\"en_US\":\"\",\"ja_JP\":\"\",\"zh_CN\":\"\",\"zh_TW\":\"\"}}",
          "countryName": {"en_US": n.en_US, "ja_JP": n.ja_JP, "zh_CN": n.zh_CN, "zh_TW": n.zh_TW },
          "countryCode": n.country,
          "restrictStatus": this.form.restrictStatus
        })
      )||[];
      this.viewLocaleList = this.viewLocaleList.concat(areasList);
    },
    changeLocale(e){
      this.form.saleArea = e;
    },
    setOriginalDate(v){
      if(!this.form.startDate)return false
      return v.getTime() >= new Date(this.form.startDate).getTime();
    },
    beforeOptions(v){
      if(!this.form.endDate)return false
      return v.getTime() >= new Date(this.form.endDate).getTime();
    },
    backOptions(v){
      return v.getTime() <= new Date(this.form.startDate).getTime();
    },
    preSaleOptions(v){
      return v.getTime() > new Date(this.form.startDate).getTime();
    }
  }
}
